package com.pig4cloud.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.entity.BusInterfaceRecordEntity;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.service.BusInterfaceRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口状态记录表
 *
 * @author pig
 * @date 2025-04-03 23:14:20
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/busInterfaceRecord" )
@Tag(description = "busInterfaceRecord" , name = "接口状态记录表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BusInterfaceRecordController {

    private final BusInterfaceRecordService busInterfaceRecordService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param busInterfaceRecord 接口状态记录表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('pig_busInterfaceRecord_view')" )
    public R getBusInterfaceRecordPage(@ParameterObject Page page, @ParameterObject BusInterfaceRecordEntity busInterfaceRecord) {
        LambdaQueryWrapper<BusInterfaceRecordEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(busInterfaceRecordService.page(page, wrapper));
    }


    /**
     * 通过id查询接口状态记录表
     * @param recordId id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{recordId}" )
    @PreAuthorize("@pms.hasPermission('pig_busInterfaceRecord_view')" )
    public R getById(@PathVariable("recordId" ) Long recordId) {
        return R.ok(busInterfaceRecordService.getById(recordId));
    }

    /**
     * 新增接口状态记录表
     * @param busInterfaceRecord 接口状态记录表
     * @return R
     */
    @Operation(summary = "新增接口状态记录表" , description = "新增接口状态记录表" )
    @SysLog("新增接口状态记录表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pig_busInterfaceRecord_add')" )
    public R save(@RequestBody BusInterfaceRecordEntity busInterfaceRecord) {
        return R.ok(busInterfaceRecordService.save(busInterfaceRecord));
    }

    /**
     * 修改接口状态记录表
     * @param busInterfaceRecord 接口状态记录表
     * @return R
     */
    @Operation(summary = "修改接口状态记录表" , description = "修改接口状态记录表" )
    @SysLog("修改接口状态记录表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pig_busInterfaceRecord_edit')" )
    public R updateById(@RequestBody BusInterfaceRecordEntity busInterfaceRecord) {
        return R.ok(busInterfaceRecordService.updateById(busInterfaceRecord));
    }

    /**
     * 通过id删除接口状态记录表
     * @param ids recordId列表
     * @return R
     */
    @Operation(summary = "通过id删除接口状态记录表" , description = "通过id删除接口状态记录表" )
    @SysLog("通过id删除接口状态记录表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('pig_busInterfaceRecord_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(busInterfaceRecordService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param busInterfaceRecord 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pig_busInterfaceRecord_export')" )
    public List<BusInterfaceRecordEntity> export(BusInterfaceRecordEntity busInterfaceRecord,Long[] ids) {
        return busInterfaceRecordService.list(Wrappers.lambdaQuery(busInterfaceRecord).in(ArrayUtil.isNotEmpty(ids), BusInterfaceRecordEntity::getRecordId, ids));
    }
}